package com.ycsj.goldmedalnewconcept.teacher.activity;

import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.mob.MobSDK;
import com.ycsj.common.base.BaseActivity;
import com.ycsj.common.manager.BannerManager;
import com.ycsj.common.manager.PickChooseManager;
import com.ycsj.goldmedalnewconcept.R;
import com.ycsj.goldmedalnewconcept.common.HttpAddress;
import com.ycsj.goldmedalnewconcept.common.bean.EventInfo;
import com.ycsj.goldmedalnewconcept.common.util.AccountUtils;
import com.youth.banner.Banner;
import com.youth.banner.Transformer;
import com.youth.banner.listener.OnBannerListener;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
import cn.sharesdk.framework.Platform;
import cn.sharesdk.onekeyshare.OnekeyShare;
import cn.sharesdk.onekeyshare.ShareContentCustomizeCallback;
import cn.sharesdk.sina.weibo.SinaWeibo;

/**
 * 教师端活动详情
 * Created by Administrator on 2019/4/2 0002.
 */

public class TeacherEventDetailsActivity extends BaseActivity {

    @BindView(R.id.tv_title)
    TextView tvTitle;

    @BindView(R.id.lv_edit)
    ImageView lv_edit;

    @BindView(R.id.banner)
    Banner banner;
    @BindView(R.id.tv_num)
    TextView tvNum;

    //活动名称
    @BindView(R.id.tv_name)
    TextView tvName;

    // 新概念1班,新概念2班
    @BindView(R.id.tv_target)
    TextView tvTarget;

    //时间
    @BindView(R.id.tv_time)
    TextView tvTime;

    //地址
    @BindView(R.id.tv_address)
    TextView tvAddress;
    //活动内容
    @BindView(R.id.tv_content)
    TextView tvContent;

    //查看报名名单
    @BindView(R.id.bt_look_people)
    Button btLookPeople;

    @BindView(R.id.ll_conent)
    LinearLayout llConent;

    @BindView(R.id.rl_loading)
    RelativeLayout rlLoading;

    //页面传递的对象
    private EventInfo.ListBean info;
    private String logo;

    @Override
    protected int getContentLayout() {
        return R.layout.activity_teacher_event_details;
    }

    @Override
    protected void init() {

        //初始化状态栏
        initStateBar();

        //获取Intent对象
        initIntent();

        //初始化视图
        initTitle();

        //设置页面其它的数据
        setPageData();

    }

    /**
     * 初始化视图
     */
    public void initTitle() {
        tvTitle.setText("活动报名");


    }

    /**
     * 获取Intent对象
     */
    private void initIntent() {
        info = (EventInfo.ListBean) getIntent().getSerializableExtra("data");

        if ("公开".equals(info.getTarget())) {
            lv_edit.setImageResource(R.drawable.base_share);
            lv_edit.setVisibility(View.VISIBLE);
        } else {
            lv_edit.setVisibility(View.GONE);
        }

        if (TextUtils.isEmpty(AccountUtils.getLogo(this))) {
            logo = "http://139.129.57.83:8080/Appandroid/applogo.png";
        } else {
            TextUtils.isEmpty(AccountUtils.getLogo(this));
        }
    }

    /**
     * 设置轮播图的数据
     */
    private void setBanner() {
        if (info != null) {
            final List<String> bannerUrls = new ArrayList<>();
            if (!TextUtils.isEmpty(info.getPicture1())) {
                bannerUrls.add(info.getPicture1());
            }
            if (!TextUtils.isEmpty(info.getPicture2())) {
                bannerUrls.add(info.getPicture2());
            }

            if (!TextUtils.isEmpty(info.getPicture3())) {
                bannerUrls.add(info.getPicture3());
            }

            if (!TextUtils.isEmpty(info.getPicture4())) {
                bannerUrls.add(info.getPicture4());
            }
            if (!TextUtils.isEmpty(info.getPicture5())) {
                bannerUrls.add(info.getPicture5());
            }
            if (!TextUtils.isEmpty(info.getPicture6())) {
                bannerUrls.add(info.getPicture6());
            }
            if (!TextUtils.isEmpty(info.getPicture7())) {
                bannerUrls.add(info.getPicture7());
            }
            if (!TextUtils.isEmpty(info.getPicture8())) {
                bannerUrls.add(info.getPicture8());
            }
            if (!TextUtils.isEmpty(info.getPicture9())) {
                bannerUrls.add(info.getPicture9());
            }

            if (bannerUrls.size() == 0) {
                List<Integer> imgs = new ArrayList<>();
                imgs.add(R.drawable.base_event_def);
                //证明没有上传图片,那么现实默认的本地图片
                BannerManager.setDataLocal(banner, imgs, Transformer.ZoomIn);
            } else {
                BannerManager.setData(banner, true, bannerUrls, Transformer.ZoomIn);
                //设置点击事件
                banner.setOnBannerListener(new OnBannerListener() {
                    @Override
                    public void OnBannerClick(int position) {
                        PickChooseManager.startShowBigActivity(TeacherEventDetailsActivity.this, bannerUrls, position);
                    }
                });
            }
        }
    }

    /**
     * 设置页面其它显示数据
     */
    private void setPageData() {

        rlLoading.setVisibility(View.GONE);

        llConent.setVisibility(View.VISIBLE);

        if (info != null) {

            //设置轮播图的数据
            setBanner();

            //轮播图的标题
            tvName.setText(info.getTag());

            //报名人数
            if (TextUtils.isEmpty(info.getCount())) {
                tvNum.setText("0");
            } else {
                tvNum.setText(info.getCount());
            }

            //活动对象
            tvTarget.setText(info.getTarget());

            //活动时间
            tvTime.setText(info.getBegintime() + "  至  " + info.getEndtime());

            //活动地点
            tvAddress.setText(info.getAddress());

            //活动简介
            tvContent.setText(info.getContent());

             //根据是否有人报名来设置报名按钮的背景以及是否可点击的状态
            if("0".equals(info.getCount())){
                btLookPeople.setEnabled(false);
                btLookPeople.setBackgroundResource(R.drawable.parent_bt_nolook_people);
            }else{
                btLookPeople.setEnabled(true);
                btLookPeople.setBackgroundResource(R.drawable.parent_bt_look_people);
            }
        }
    }

    /**
     * 点击事件
     *
     * @param v
     */
    @OnClick({R.id.iv_back, R.id.bt_look_people, R.id.lv_edit})
    public void onClick(View v) {
        int id = v.getId();
        if (id == R.id.iv_back) {
            //返回
            finish();
        } else if (id == R.id.bt_look_people) {
            //报名名单
            TeacherEventPeoplesActivity.goActivity(this,info.getRowId()+"","");

        }else if (id == R.id.lv_edit) {
            share();
        }
    }

    /**
     * 分享活动
     */
    private void share() {
        MobSDK.init(this, "12c24ea0b59b0",
                "aae63565fbd69b1acbfcdd0d5ac0b35c");
        OnekeyShare oks = new OnekeyShare();
        // 关闭sso授权
        oks.disableSSOWhenAuthorize();
        oks.setShareContentCustomizeCallback(new ShareContentCustomizeCallback() {

            @Override
            public void onShare(Platform platform, Platform.ShareParams paramsToShare) {
                String title;
                String text;
                if(TextUtils.isEmpty(AccountUtils.getSchoolName(TeacherEventDetailsActivity.this))){
                    title =  info.getTag()+"活动正在报名中，快来参与吧";
                    text = "点击访问详情";
                }else{
                    title = AccountUtils.getSchoolName(TeacherEventDetailsActivity.this)
                            + "的"+info.getTag()+"活动正在报名中，快来参与吧";
                    text =  AccountUtils.getSchoolName(TeacherEventDetailsActivity.this);
                }


                if (SinaWeibo.NAME.equals(platform.getName())) {
                    paramsToShare.setTitle(getString(R.string.share));
                    paramsToShare.setTitleUrl(HttpAddress.EVENTSHARE
                            + "/JPXB/sharePage/pages/JPXGN/activePage/activePage.html?rowId="
                            + info.getRowId() + "&school=" + AccountUtils.getSchoolId(TeacherEventDetailsActivity.this));
                    paramsToShare.setTitle(title);
                    paramsToShare.setText(text);
                    paramsToShare
                            .setImageUrl(logo);
                    paramsToShare.setUrl(HttpAddress.EVENTSHARE
                            + "/JPXB/sharePage/pages/JPXGN/activePage/activePage.html?rowId="
                            + info.getRowId() + "&school=" + AccountUtils.getSchoolId(TeacherEventDetailsActivity.this));
                    paramsToShare.setComment("");
                    paramsToShare.setSite("金牌君");
                    paramsToShare.setSiteUrl(HttpAddress.EVENTSHARE
                            + "/JPXB/sharePage/pages/JPXGN/activePage/activePage.html?rowId="
                            + info.getRowId() + "&school=" + AccountUtils.getSchoolId(TeacherEventDetailsActivity.this));
                } else {
                    paramsToShare.setShareType(Platform.SHARE_WEBPAGE);
                    paramsToShare.setTitle(getString(R.string.share));
                    paramsToShare.setTitleUrl(HttpAddress.EVENTSHARE
                            + "/JPXB/sharePage/pages/JPXGN/activePage/activePage.html?rowId="
                            + info.getRowId() + "&school=" + AccountUtils.getSchoolId(TeacherEventDetailsActivity.this));
                    paramsToShare.setTitle(title);
                    paramsToShare.setText(text);
                    paramsToShare
                            .setImageUrl(logo);

                    paramsToShare.setUrl(HttpAddress.EVENTSHARE
                            + "/JPXB/sharePage/pages/JPXGN/activePage/activePage.html?rowId="
                            + info.getRowId() + "&school=" + AccountUtils.getSchoolId(TeacherEventDetailsActivity.this));
                    paramsToShare.setComment("");
                    paramsToShare.setSite("金牌君");
                    paramsToShare.setSiteUrl(HttpAddress.EVENTSHARE
                            + "/JPXB/sharePage/pages/JPXGN/activePage/activePage.html?rowId="
                            + info.getRowId() + "&school=" + AccountUtils.getSchoolId(TeacherEventDetailsActivity.this));
                }
            }
        });
        oks.show(this);
    }

}
